#include "GhostSelect.h"
#include "../../../GamePlayConfig.h"
#include "../../../Scenes/Play/PlayManager.h"
#include "../../../Framework/SoundManager.h"

GhostSelect::GhostSelect()
{
    //GhostObject::GhostObject();
}
int GhostSelect::getValue()
{
    return m_value;
}
void GhostSelect::initalize(EGhostColor color, Vec2 position, float scale, int value,bool isAutoChangeState,bool isAutoMove)
{
	m_value = value;

    GhostObject::initalize(color,EGhostFaceState::kGhostFaceState_Free,EGhostHandState::kGhostHandState_None, EGhostEyeColor::kGhostEyeColor_Random, position, scale, isAutoChangeState);
	initValue();
    
    if(isAutoMove)
    {
        auto func = CallFuncN::create(CC_CALLBACK_0(GhostSelect::Move,this));
        this->runAction(RepeatForever::create(Sequence::create(func,DelayTime::create(random(3.0f,8.0f)), NULL)));
        
    }
}
void GhostSelect::Move()
{
    Vec2 p1 = Vec2(this->getPosition().x + random(-m_body->getContentSize().width*m_scale/3,m_body->getContentSize().width*m_scale/3),this->getPosition().y + random(-m_body->getContentSize().height*m_scale/3,m_body->getContentSize().height*m_scale/3));
    Vec2 p2 = Vec2(this->getPosition().x + random(-m_body->getContentSize().width*m_scale/3,m_body->getContentSize().width*m_scale/3),this->getPosition().y + random(-m_body->getContentSize().height*m_scale/3,m_body->getContentSize().height*m_scale/3));
    
    Vec2 p3 = this->getPosition();
    ccBezierConfig config;
    config.controlPoint_1 = p1;
    config.controlPoint_2 = p2;
    config.endPosition = p3;
    
    float time =random(3.0f,8.0f);
    m_body->runAction(BezierTo::create(time, config));
    
}
void GhostSelect::initValue()
{
	float newWidthGhost = kGhostSpriteWidth*m_scale;
	float newHeightGhost = kGhostSpriteHeight*m_scale;

    btnValue = Button::create("messagebox.png");
    //btnValue->setScale(m_scale);
	//btnValue->loadTextures("messagebox.png", "messagebox.png");
	btnValue->setTitleText(String::createWithFormat("%d", m_value)->getCString());
	btnValue->setTitleColor(Color3B::BLACK);
	
    //btnValue->getTitleRenderer()->setPosition(Vec2(btnValue->getContentSize().width/2,btnValue->getContentSize().height*0.0f));
    btnValue->setAnchorPoint(Vec2(0.5f,0.7f));
	btnValue->setTitleFontSize(120);
    btnValue->setTitleFontName(kFont_ADLIBN);
	btnValue->setPosition(Vec2(0, newHeightGhost + btnValue->getContentSize().height));
    
	btnValue->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(0.7f , 1.5f* m_scale), ScaleTo::create(1.0f, 1.2f* m_scale), NULL)));

	btnValue->addClickEventListener(CC_CALLBACK_1(GhostSelect::tapValue, this));
    m_body->addClickEventListener(CC_CALLBACK_1(GhostSelect::tapValue,this));
	m_body->addChild(btnValue);
}

void GhostSelect::tapValue(cocos2d::Ref *pSender)
{
    if(PlayManager::m_isTutorial && m_value != GamePlayConfig::GetInstance()->m_hiddenNumber)
        return;
    
    SoundManager::getInstance()->playSound(ESound::kSound_Click);
    GamePlayConfig::GetInstance()->m_selectNumber = m_value;
}
void GhostSelect::changeState()
{}